// fast4j - Fast & Agile Service Tools for Java
// Copyright (C) 2007 Alexandre ROMAN
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

// $Id: Client.java 36 2007-03-27 22:02:40Z alexandre.roman $

package client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.api.CodecService;
import service.api.EchoService;

/**
 * Service client.
 * @author alexandre.roman
 */
public class Client {
    /**
     * Calls service methods.
     * @param args not used
     * @throws Exception no error should be thrown
     */
    public static void main(String[] args) throws Exception {
        final Logger log = LoggerFactory.getLogger(Client.class);
        
        // create a Spring client context
        final ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:client/client.xml");
        ctx.registerShutdownHook();
        
        // get EchoService dynamic proxy
        final EchoService echoService = (EchoService) ctx
                .getBean("echoService");
        log.info("EchoService.echo('hello') => {}", echoService.echo("hello"));
        
        try {
            log.info("Calling EchoService with null argument...");
            echoService.echo(null);
            log.error("IllegalArgumentException was expected");
        } catch (IllegalArgumentException e) {
            log.info("Fine! We got IllegalArgumentException as expected", e);
        }
        
        // get CodecService dynamic proxy
        final CodecService codecService = (CodecService) ctx
                .getBean("codecService");
        log.info("CodecService.toBase64('hello'.getBytes('UTF-8')) => {}",
            codecService.toBase64("hello".getBytes("UTF-8")));
    }
}
